<!--
 * @Description: 签约合同
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2023-06-27 17:54:22
 * @LastEditTime: 2023-06-28 17:50:51
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
-->

<template>
  <div class="lb-examine-goods">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="toShowDialog"
          v-hasPermi="`${$route.name}-toFddSign`"
          >{{ $t('action.toFddSign') }}</lb-button
        >
      </el-row>
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm.list"
          ref="listForm"
        >
          <el-form-item label="输入查询" prop="coach_name">
            <el-input
              v-model="searchForm.list.coach_name"
              :placeholder="'请输入' + $t('action.attendantName') + '姓名'"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1, 'list')"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('list')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading.list"
        :data="tableData.list"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" fixed></el-table-column>
        <el-table-column
          prop="coach_name"
          :label="$t('action.attendantName') + '姓名'"
          min-width="120"
        ></el-table-column>
        <el-table-column prop="work_img" label="封面图">
          <template slot-scope="scope">
            <lb-image :src="scope.row.work_img" />
          </template>
        </el-table-column>
        <el-table-column
          prop="agreement_title"
          label="合同名称"
          min-width="150"
        >
        </el-table-column>
        <el-table-column prop="status" label="合同状态">
          <template slot-scope="scope">
            {{ statusType[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="120">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="end_time" label="到期时间" min-width="120">
          <template slot-scope="scope" v-if="scope.row.end_time">
            <p>{{ scope.row.end_time | handleTime(1) }}</p>
            <p>{{ scope.row.end_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="toFddUrl(scope.row.viewpdf_url)"
                v-hasPermi="`${$route.name}-fddSignUrl`"
                v-show="scope.row.viewpdf_url"
                >{{ $t('action.fddSignUrl') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="success"
                @click="toFddAuth(scope.row.coach_id)"
                v-hasPermi="`${$route.name}-fddMerchantSign`"
                v-show="scope.row.status === 1"
                >{{ $t('action.fddMerchantSign') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id, scope.row.status)"
                v-hasPermi="`${$route.name}-delete`"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.list.page"
        :pageSize="searchForm.list.limit"
        :total="total.list"
        @handleSizeChange="handleSizeChange($event, 'list')"
        @handleCurrentChange="handleCurrentChange($event, 'list')"
      >
      </lb-page>

      <el-dialog
        :title="$t('action.toFddSign')"
        :visible.sync="showDialog"
        width="800px"
        center
      >
        <el-row class="page-search-form">
          <el-form
            @submit.native.prevent
            :inline="true"
            :model="searchForm.coach"
            ref="coachForm"
          >
            <el-form-item label="输入查询" prop="coach_name">
              <el-input
                v-model="searchForm.coach.coach_name"
                :placeholder="'请输入' + $t('action.attendantName') + '姓名'"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <lb-button
                size="medium"
                type="primary"
                icon="el-icon-search"
                style="margin-right: 5px"
                @click="getTableDataList(1, 'coach')"
                >{{ $t('action.search') }}</lb-button
              >
              <lb-button
                size="medium"
                icon="el-icon-refresh-left"
                style="margin-right: 5px"
                @click="resetForm('coach')"
                >{{ $t('action.reset') }}</lb-button
              >
            </el-form-item>
          </el-form>
        </el-row>
        <el-table
          v-loading="loading.coach"
          :data="tableData.coach"
          ref="singleTable"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          tooltip-effect="dark"
          style="width: 100%"
          highlight-current-row
          @current-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="ID" fixed></el-table-column>
          <el-table-column prop="work_img" label="头像">
            <template slot-scope="scope">
              <lb-image :src="scope.row.work_img" />
            </template>
          </el-table-column>
          <el-table-column prop="coach_name" label="姓名"></el-table-column>
        </el-table>
        <lb-page
          :batch="false"
          :page="searchForm.coach.page"
          :pageSize="searchForm.coach.limit"
          :total="total.coach"
          @handleSizeChange="handleSizeChange($event, 'coach')"
          @handleCurrentChange="handleCurrentChange($event, 'coach')"
        >
        </lb-page>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button
            type="primary"
            @click="handleDialogConfirm"
            v-preventReClick
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import moment from 'moment'
export default {
  data () {
    return {
      statusType: { '-1': '删除', 0: '到期', 1: '商家待签署', 2: '用户待签署', 3: '待归档', 4: '履约中' },
      loading: { list: false, coach: false },
      searchForm: {
        list: {
          page: 1,
          limit: 10,
          coach_name: ''
        },
        coach: {
          page: 1,
          limit: 10,
          coach_name: ''
        }
      },
      tableData: { list: [], coach: [] },
      total: { list: 0, coach: 0 },
      currentRow: {},
      showDialog: false
    }
  },
  activated () {
    this.getTableDataList(1, 'list')
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    resetForm (form) {
      let name = `${form}Form`
      this.$refs[name].resetFields()
      this.getTableDataList(1, form)
    },
    handleSizeChange (val, key) {
      this.searchForm[key].limit = val
      this.handleCurrentChange(1, key)
    },
    handleCurrentChange (val, key) {
      this.searchForm[key].page = val
      this.getTableDataList('', key)
    },
    async getTableDataList (flag, key) {
      if (flag) this.searchForm[key].page = 1
      this.tableData[key] = []
      this.loading[key] = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm[key]))
      let methodArr = {
        list: { methodKey: 'system', methodModel: 'agreementList' },
        coach: { methodKey: 'system', methodModel: 'noAgreementCoach' }
      }
      let { methodKey, methodModel } = methodArr[key]
      let { code, data } = await this.$api[methodKey][methodModel](searchForm)
      this.loading[key] = false
      if (code !== 200) return
      this.tableData[key] = data.data
      this.total[key] = data.total
    },
    confirmDel (id, status) {
      let msg = status > 2 ? `已签约合同` : `已发起合同`
      const h = this.$createElement
      this.$confirm(h('p', null, [
        h('p', null, `你确认要删除吗？`),
        h('p', { style: 'font-size:13px;line-height:1.2;color: red' }, `删除后，${msg}将作废！`)
      ]), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1, 'list')
      }).catch(() => {

      })
    },
    async updateItem (id, status, key) {
      this.$api.system.delAgreement({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm[key].page = this.searchForm[key].page < Math.ceil((this.total[key] - 1) / this.searchForm[key].limit) ? this.searchForm[key].page : Math.ceil((this.total[key] - 1) / this.searchForm[key].limit)
            this.getTableDataList('', key)
          }
        } else {
          if (status === -1) return
          this.getTableDataList('', key)
        }
      })
    },
    toFddAuth (id) {
      this.currentRow = { id }
      this.handleDialogConfirm()
    },
    toFddUrl (url) {
      window.open(url)
    },
    async toShowDialog () {
      this.searchForm.coach.coach_name = ''
      await this.getTableDataList(1, 'coach')
      this.showDialog = !this.showDialog
    },
    handleSelectionChange (val) {
      this.currentRow = val
    },
    async handleDialogConfirm () {
      if (this.currentRow === null || !this.currentRow.id) {
        this.$message.error(`请选择${this.$t('action.attendantName')}`)
        return
      }
      let { id } = this.currentRow
      let { code, data } = await this.$api.system.Extsign({ coach_id: id })
      if (code !== 200) return
      this.toFddUrl(data)
      if (!this.showDialog) return
      this.showDialog = false
      this.getTableDataList(1, 'list')
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-table {
  .table-goods-info {
    width: 280px;
    height: 80px;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    font-size: 12px;

    .goods-info-r {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      p {
        width: 210px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .price {
        color: red;
      }
    }

    .el-image {
      min-width: 70px;
      height: 70px;
      margin-right: 10px;
    }

    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
